<template>
  <view class="background">
    <hx-navbar1 title="拉环抽奖" :back="true" :fixed="false" :color="knavTitleColor" barPlaceholder="hidden"
               transparent="auto"></hx-navbar1>
    <view class="ring_title">拉环抽奖</view>
    <view class="ring_reward" @click="see_reward">查看我的奖品&nbsp;&nbsp;>&nbsp;&nbsp;>&nbsp;&nbsp;</view>
    <view class="tiger_container">
      <view class="ring_num">您获得{{ reward_num }}次抽奖机会</view>
      <view class="tiger_position">
        <SlotMachine
            ref="myLucky"
            width="500rpx"
            height="235rpx"
            :prizes="prizes"
            :slots="slots"
            :defaultConfig="defaultConfig"
            @end="endCallBack"
        />
      </view>
      <button plain class="click_button" @click="clickReward" :disabled="unClick"></button>
    </view>
    <view class="ring_content_container">
      <view class="ring_content">
        <view>抽奖须知：</view>
        <view>1、本活动为啤酒免费喝拉环抽奖活动，每位参与者都有机会赢取丰厚的奖品；</view>
        <view>2、8.88万、18.88万，58.88万和 188.88万需要线下领取，您可拨打客服电话010-67520714或等待第二天客服与您联系；</view>
        <view>3、严禁通过任何作弊手段来获取奖励；</view>
        <view>4、奖品必须由本人领取，若因他人领取而引发纠纷，本公司将不予以裁决。</view>
        <view>5、抽奖机会仅保留48小时，请您及时参与抽奖。</view>
      </view>
    </view>

    <view class="bg_black" v-show="show_modal" @click="show_modal = false">
      <view v-if="reward_list">
        <view class="result_title">
          <view>恭喜您获得{{reward_list.name}}！</view>
          <view>初始价值总计{{reward_list.totalValue|fp}}元</view>
        </view>
        <view class="result_container">
          <view class="result_item" v-for="(item,index) in reward_list.lotteryPrizeVOS" :key="index">
            <image :src="item.image"></image>
            <view class="result_item_content">初始价值：{{item.unitPrice|fp}}元</view>
            <view class="result_item_content">数量：{{item.quantity}}</view>
          </view>
        </view>
      </view>
      <view v-else class="no_data">暂无数据</view>
      <view class="reward_call">
        <view class="reward_call_title">领奖须知</view>
        <view class="reward_call_content">8.88万、18.88万，58.88万和188.88万需要线下领取，您可拨打客服电话或第二天等待客服联系您！</view>
        <view class="reward_call_button" @click="getAll">去领取</view>
      </view>
    </view>
  </view>
</template>

<script>
import SlotMachine from '@/components/@lucky-canvas/uni/slot-machine'
import hxNavbar1 from "@/components/hx-navbar1/hx-navbar.vue"
export default {
  components: { SlotMachine,hxNavbar1 },
  onLoad(options) {
    this.token = options.token
    this.codeTicket = options.code_ticket
    this.jscode = options.code
    uni.setStorageSync('every_code', "1");
  },
  onShow() {
    this.getMyToken()
  },
  data () {
    return {
      token: '',
      codeTicket: '',
      jscode: '',
      jscode_session: '',
      unClick: false,   //抽奖时不允许点击
      click_status: 1, //1是允许点击，2是没有抽奖次数，3是系统报错
      reward_list: {},
      reward_num: 0,
      show_modal: false,
      defaultConfig: {
        colSpacing: 20,
        rowSpacing: 10
      },
      prizes: [
        {
          background: 'RGBA(253, 235, 201, 1)',
          borderRadius: '15px',
          imgs: [{
            top: 3,
            height: '90%',
            width:'90%',
            src: '../../static/ring/reward_1.png'
          }]
        },
        {
          background: 'RGBA(253, 235, 201, 1)',
          borderRadius: '15px',
          imgs: [{
            top: 3,
            height: '90%',
            width:'90%',
            src: '../../static/ring/reward_2.png'
          }]
        },
        {
          background: 'RGBA(253, 235, 201, 1)',
          borderRadius: '15px',
          imgs: [{
            top: 3,
            height: '90%',
            width:'90%',
            src: '../../static/ring/reward_3.png'
          }]
        },
        ],
      slots: [
        { speed: 5 },
        { speed: 8 },
        { speed: 10 },
      ]
    }
  },
  methods: {
    getMyToken() {
      const that = this
      if(!this.codeTicket || !this.jscode) {
        uni.showModal({
          title: '提示',
          content: '请重新扫码!',
          showCancel: false,
          confirmText: '确定'
        });
        this.click_status = 3;
        return false;
      }
      // 登录
      this.$api.token_to_token({
        token: this.token
      }, res => {
        if (res.message) {
          uni.showModal({
            title: '提示',
            content: res.message,
            showCancel: false,
            confirmText: '确定'
          });
        } else {
          that.$db.set('userToken', res.data.token)
          if (that.jscode_session === that.jscode) {
            return false
          } else {
            setTimeout(() => {
              // 更新页面抽奖次数
              that.$api.redirect_get_token({
                codeTicket: that.codeTicket,
                jscode: that.jscode,
              }, res_init => {
                if (res_init.message) {
                  uni.showModal({
                    title: '提示',
                    content: res_init.message,
                    showCancel: false,
                    confirmText: '确定'
                  });
                  that.click_status = 3
                }else if (res_init.data) {
                  that.jscode_session = that.jscode
                  that.reward_num = res_init.data.lotteryNum;
                  if (res_init.data.status === 1) {
                    uni.showModal({
                      title: '提示',
                      content: '已扫码',
                      showCancel: false,
                      confirmText: '确定'
                    });
                  }
                  if (!res_init.data.lotteryNum) {
                    that.click_status = 2
                  }
                }
              })
            }, 100);
          }
        }
      })
    },
    getAll() {
      this.show_modal = false
      uni.redirectTo({
        url: '/pages/ring/ring_reward',
      })
    },
    startCallBack() {
      this.reward_list = {}
      const that = this
      if (this.click_status === 3 || this.click_status === 2) {
        return false
      }
      this.unClick = true
      this.$api.people_get_reward(null, res_init => {
        if (res_init.message) {
          uni.showModal({
            title: '提示',
            content: res_init.message,
            showCancel: false,
            confirmText: '确定'
          });
        } else if (res_init.data) {
          that.reward_list = res_init.data;
          that.reward_num = res_init.data.lotteryNum
          if (!res_init.data.lotteryNum) {
            that.click_status = 2
          }
          that.$refs.myLucky.play()
          setTimeout(() => {
            const index = [1,1,2]
            that.$refs.myLucky.stop(index)
          }, 3000)
          setTimeout(() => {
            that.unClick = false
          }, 3100)
        }
      });
    },
    clickReward() {
      this.startCallBack();
    },
    endCallBack() {
      this.show_modal = true
    },
    see_reward() {
      uni.navigateTo({
        url: '/pages/ring/ring_reward'
      })
    },
  }
}
</script>

<style lang="scss">
uni-button[disabled]:not([type]), uni-button[disabled][type=default] {
  background-color: transparent;
}
.click_button {
  border: 0;
  position: absolute;
  bottom: 100rpx;
  height: 150rpx;
  margin: 0 auto;
  width: 60%;
  left: 20%;
  background: transparent;
}

.no_data {
  margin-top: 200rpx;
  text-align: center;
  font-size: 32rpx;
}
.background {
  width: 100%;
  height: 100vh;
  background: url("@/static/ring/background.png") no-repeat;
  background-size: cover;
  .ring_num {
    color: rgba(213, 17, 73, 1);
    position: absolute;
    z-index: 100;
    left: 59%;
    top: 14%;
    transform: translate(-50%, -50%);
  }
  .ring_title {
    margin: 88rpx auto 48rpx auto;
    font-family: HuXiaoBo, HuXiaoBo;
    font-weight: normal;
    font-size: 80rpx;
    color: rgba(255, 235, 198, 1);
    text-align: center;
    font-style: normal;
  }
  .tiger_container {
    width: 750rpx;
    height: 670rpx;
    position: relative;
    background: url("@/static/ring/tiger.png") no-repeat;
    background-size: contain;
    .tiger_position {
      background-color: RGBA(159, 0, 37, 1);
      position: absolute;
      top: 172rpx;
      left: 130rpx;
    }
  }
  .ring_reward {
    margin: 0 auto 35rpx auto;
    width: 340rpx;
    height: 68rpx;
    background: linear-gradient( 180deg, #FDF9EF 0%, #FFEBC6 100%);
    border-radius: 34rpx;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 30rpx;
    color: #333333;
    line-height: 68rpx;
    text-align: center;
    font-style: normal;
    box-shadow: 7rpx 10rpx rgba(220, 19, 21, 1);
  }
  .ring_content_container {
    width: 100%;
    background-color: RGBA(242, 23, 70, 1);
    padding: 50rpx 0 25rpx 0;
    .ring_content {
      color: rgba(255, 236, 175, 1);
      padding: 35rpx;
      width: 648rpx;
      margin: 0 auto;
      background-color: RGBA(226, 0, 44, 1);
      border-radius: 30rpx;
    }
  }
  .bg_black {
    top: 0;
    left: 0;
    z-index: 1000;
    position: fixed;
    width: 100%;
    height: 100vh;
    //background-color: RGBA(0, 0, 0, .7);
    background: url("@/static/ring/reward_bg.png") no-repeat;
    background-size: cover;

    .result_container {
      margin-top: 100rpx;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      .result_item {
        image {
          width: 240rpx;
          height: 210rpx;
        }
        &_content {
          color: #ffffff;
          text-align: center;
        }
      }
    }
    .result_title {
      text-align: center;
      color: RGBA(249, 229, 182, 1);
      font-weight: bold;
      font-size: 50rpx;
      margin: 30% auto 0 auto;
      //background: url('@/static/ring/reward_title.png') no-repeat;
      //background-size: contain;
      height: 138rpx;
    }
    .reward_call {
      color: #ffffff;
      &_title {
        text-align: center;
        position: relative;
        margin: 80rpx 0 60rpx 0;
      }
      &_title::after {
        content: '';
        left: 150rpx;
        top: 20rpx;
        position: absolute;
        width: 120rpx;
        height: 5rpx;
        background-color: #ffffff;
      }
      &_title::before {
        top: 20rpx;
        right: 150rpx;
        content: '';
        position: absolute;
        width: 120rpx;
        height: 5rpx;
        background-color: #ffffff;
      }
      &_content {
        width: 580rpx;
        margin: 0 auto;
      }
      &_button {
        text-align: center;
        width: 400rpx;
        height: 100rpx;
        line-height: 100rpx;
        background: rgba(255,255,255,0.18);
        border-radius: 54rpx;
        border: 2rpx solid #FFFFFF;
        margin: 70rpx auto 0 auto;
      }
    }
  }
}

</style>
